<?php

/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile
?>
<?php
/**
 * Product list template
 *
 * @var $block \Magento\Catalog\Block\Product\ListProduct
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');

$outofstockEnable = $this->getConfig('outofstock_product/outofstockproduct/enableoutofstock');
$outofstockLabelImage = $this->getConfig('outofstock_product/outofstockproduct/showstock_label_image');
$outofstockTitle = $this->getConfig('outofstock_product/outofstockproduct/titleoutofstock');
$outofstockImage = $this->getConfig('outofstock_product/outofstockproduct/imageoutofstock');
$imageUrl = $this->getMediaUrl() . 'outofstockimage/' . $outofstockImage;
$outofstockPositionLabel = $this->getConfig('outofstock_product/outofstockproduct/showstock_position_label');
$outofstockPositionImage = $this->getConfig('outofstock_product/outofstockproduct/showstock_position_image');
$outofstockImageHeight = $this->getConfig('outofstock_product/outofstockproduct/imageheightoutofstock');
$outofstockImagewidth = $this->getConfig('outofstock_product/outofstockproduct/imagewidthoutofstock');
?>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php echo $block->getToolbarHtml() ?>
    <?php echo $block->getAdditionalHtml() ?>
    <?php
    if ($block->getMode() == 'grid') {
        $viewMode = 'grid';
        $image = 'category_page_grid';
        $showDescription = false;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode = 'list';
        $image = 'category_page_list';
        $showDescription = true;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
    }
    /**
     * Position for actions regarding image size changing in vde if needed
     */
    $pos = $block->getPositioned();
    ?>
    <div class="products wrapper <?php /* @escapeNotVerified */ echo $viewMode; ?> products-<?php /* @escapeNotVerified */ echo $viewMode; ?>">
        <?php $iterator = 1; ?>
        <ol class="products list items product-items">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php foreach ($_productCollection as $_product): ?>
                <?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
                <div class="product-item-info" data-container="product-grid">
                    <?php
                    $productImage = $block->getImage($_product, $image);
                    if ($pos != null) {
                        $position = ' style="left:' . $productImage->getWidth() . 'px;'
                                . 'top:' . $productImage->getHeight() . 'px;"';
                    }
                    ?>
                    <?php // Product Image ?>
                    <?php if ($viewMode == 'grid') { ?>
                        <div class="product-img-block">
                        <?php } ?>
                        <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                            <?php echo $productImage->toHtml(); ?>
                            <?php
                            if (!$_product->getIsSalable()):
                                if ($outofstockEnable) {
                                    if ($outofstockLabelImage == 'label') {
                                        ?>
                                        <div class="out-of-stock-wrapper <?php echo $outofstockPositionLabel; ?>"><?php echo $outofstockTitle; ?></div>
                                        <?php
                                    }
                                    if ($outofstockLabelImage == 'image') {
                                        if ($outofstockImage) {
                                            ?>
                                            <div class="out-of-stock-wrapper <?php echo $outofstockPositionImage; ?>"><img src="<?php echo $imageUrl; ?>" height="<?php echo $outofstockImageHeight; ?>" width="<?php echo $outofstockImagewidth; ?>"/></div>
                                            <?php
                                        }
                                    }
                                }
                            endif;
                            ?>
                            <div class="pro-overlay"></div>
                            <?php if ($viewMode == 'list') { ?>
                            <?php
                            $helper = $this->helper('Solwin\Cpanel\Helper\Data');
                            $isNewLabel = $helper->getShowproductnewlabel();
                            $isSaleLabel = $helper->getShowproductsalelabel();

                            /**
                             * Check if "new" label is enabled and if product has from new date & to new date
                             */
                            if ($isNewLabel) {
                                $fromDate = $_product->getNewsFromDate();
                                $toDate = $_product->getNewsToDate();

                                if (isset($fromDate) && isset($toDate)) {
                                    $fromDate = strtotime($fromDate);
                                    $toDate = strtotime($toDate);
                                    $now = strtotime(date("Y-m-d h:m:s"));

                                    if ($fromDate <= $now && $now <= $toDate) {
                                        ?>
                                        <span class="new-lbl">New</span>
                                        <?php
                                    }
                                }
                            }
                            ?>

                            <?php
                            /**
                             * Check if "sale" label is enabled and if product has special price with from & to special price
                             */
                            if ($isSaleLabel) {
                                $special_price = number_format($_product->getSpecialPrice(), 2);
                                $regular_price = number_format($_product->getPrice(), 2);
                                if (isset($special_price) && $special_price > 0) {
                                    if ($special_price < $regular_price) {
                                        $fromSpecialDate = $_product->getSpecialFromDate();
                                        $toSpecialDate = $_product->getSpecialToDate();

                                        if (isset($fromSpecialDate) && isset($toSpecialDate)) {
                                            $fromSpecialDate = strtotime($fromSpecialDate);
                                            $toSpecialDate = strtotime($toSpecialDate);
                                            $now = strtotime(date("Y-m-d h:m:s"));

                                            if ($fromSpecialDate <= $now && $now <= $toSpecialDate) {
                                                ?>
                                                <span class="sale-lbl">Sale</span>
                                                <?php
                                            }
                                        }
                                    }
                                }
                            }
                            ?>
                            <?php } ?>
                        </a>
                        <?php if ($viewMode == 'grid') { ?>
                            <div class="product-item-inner">
                                <div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
                                    <div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
                                        <?php if ($_product->isSaleable()): ?>
                                            <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                            <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
                                                <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
                                                <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                <?php echo $block->getBlockHtml('formkey') ?>
                                                <button type="submit"
                                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                        class="action tocart primary">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                </button>
                                            </form>
                                        <?php else: ?>
                                            <?php if ($_product->getIsSalable()): ?>
                                                <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                            <?php else: ?>
                                                <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                    <div data-role="add-to-links" class="actions-secondary"<?php echo strpos($pos, $viewMode . '-secondary') ? $position : ''; ?>>
                                        <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                            <a href="#"
                                               class="action towishlist"
                                               title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                               aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                               data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
                                               data-action="add-to-wishlist"
                                               role="button">
                                                <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                            </a>
                                        <?php endif; ?>
                                        <?php
                                        $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
                                        ?>
                                        <a href="#"
                                           class="action tocompare"
                                           title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                           aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                           data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
                                           role="button">
                                            <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                        </a>
                                    </div>
                                </div>
                                <?php if ($showDescription): ?>
                                    <div class="product description product-item-description">
                                        <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                        <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" title="<?php /* @escapeNotVerified */ echo $_productNameStripped ?>"
                                           class="action more"><?php /* @escapeNotVerified */ echo __('Learn More') ?></a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php } ?>
                        <?php if ($viewMode == 'grid') { ?>
                        </div>
                    <?php } ?>
                    <?php if ($viewMode == 'grid') { ?>
                    <?php
                    $helper = $this->helper('Solwin\Cpanel\Helper\Data');
                    $isNewLabel = $helper->getShowproductnewlabel();
                    $isSaleLabel = $helper->getShowproductsalelabel();

                    /**
                     * Check if "new" label is enabled and if product has from new date & to new date
                     */
                    if ($isNewLabel) {
                        $fromDate = $_product->getNewsFromDate();
                        $toDate = $_product->getNewsToDate();

                        if (isset($fromDate) && isset($toDate)) {
                            $fromDate = strtotime($fromDate);
                            $toDate = strtotime($toDate);
                            $now = strtotime(date("Y-m-d h:m:s"));

                            if ($fromDate <= $now && $now <= $toDate) {
                                ?>
                                <span class="new-lbl">New</span>
                                <?php
                            }
                        }
                    }
                    ?>

                    <?php
                    /**
                     * Check if "sale" label is enabled and if product has special price with from & to special price
                     */
                    if ($isSaleLabel) {
                        $special_price = number_format($_product->getSpecialPrice(), 2);
                        $regular_price = number_format($_product->getPrice(), 2);
                        if (isset($special_price) && $special_price > 0) {
                            if ($special_price < $regular_price) {
                                $fromSpecialDate = $_product->getSpecialFromDate();
                                $toSpecialDate = $_product->getSpecialToDate();

                                if (isset($fromSpecialDate) && isset($toSpecialDate)) {
                                    $fromSpecialDate = strtotime($fromSpecialDate);
                                    $toSpecialDate = strtotime($toSpecialDate);
                                    $now = strtotime(date("Y-m-d h:m:s"));

                                    if ($fromSpecialDate <= $now && $now <= $toSpecialDate) {
                                        ?>
                                        <span class="sale-lbl">Sale</span>
                                        <?php
                                    }
                                }
                            }
                        }
                    }
                    ?>
                    <?php } ?>

                    <div class="product details product-item-details">
                        <div style="padding: 0 2px;"><?php echo $block->getProductDetailsHtml($_product); ?></div>
                        <?php
                        $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                        ?>
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>">
                                   <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                            </a>
                        </strong>
                        <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?php /* @escapeNotVerified */ echo $block->getProductPrice($_product) ?>

                        <?php if ($viewMode == 'list') { ?>
                            <div class="product-item-inner">
                                <div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
                                    <div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
                                        <?php if ($_product->isSaleable()): ?>
                                            <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                            <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
                                                <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
                                                <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                <?php echo $block->getBlockHtml('formkey') ?>
                                                <button type="submit"
                                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                        class="action tocart primary">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                </button>
                                            </form>
                                        <?php else: ?>
                                            <?php if ($_product->getIsSalable()): ?>
                                                <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                            <?php else: ?>
                                                <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                    <div data-role="add-to-links" class="actions-secondary"<?php echo strpos($pos, $viewMode . '-secondary') ? $position : ''; ?>>
                                        <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                            <a href="#"
                                               class="action towishlist"
                                               title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                               aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                               data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
                                               data-action="add-to-wishlist"
                                               role="button">
                                                <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                            </a>
                                        <?php endif; ?>
                                        <?php
                                        $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
                                        ?>
                                        <a href="#"
                                           class="action tocompare"
                                           title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                           aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                           data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
                                           role="button">
                                            <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                        </a>
                                    </div>
                                </div>
                                <?php if ($showDescription): ?>
                                    <div class="product description product-item-description">
                                        <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                        <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" title="<?php /* @escapeNotVerified */ echo $_productNameStripped ?>"
                                           class="action more"><?php /* @escapeNotVerified */ echo __('Learn More') ?></a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php } ?>
                    </div>
                </div>
                <?php echo($iterator == count($_productCollection) + 1) ? '</li>' : '' ?>
            <?php endforeach; ?>
        </ol>
    </div>
    <?php echo $block->getToolbarHtml() ?>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
            {
            "[data-role=tocart-form], .form.map.checkout": {
            "catalogAddToCart": {}
            }
            }
        </script>
    <?php endif; ?>
<?php endif; ?>
